/*
 * Alloy Analyzer
 * Copyright (c) 2002 Massachusetts Institute of Technology
 *
 * The Alloy Analyzer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * The Alloy Analyzer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the Alloy Analyzer; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package alloy.transl;

import java.util.Iterator;
import java.util.List;

import alloy.bool.BooleanFormula;
import alloy.bool.PartialAssignment;
import alloy.util.ObjID;

/**
 * Boolean translation of an {@link alloy.ast.Node}.  The translation is
 * a group of {@link BooleanFormula}s in terms for Boolean
 * variables allocated for relations, such that the formulas
 * compute the corresponding value of the translated {@link alloy.ast.Node}.
 */
public abstract class Transl extends ObjID {
    /** Returns the {@link BooleanFormulas} representing
	bits of the translation. */
    public abstract List getBooleanFormulas();

    /** Return the value of the node for a given Boolean assignment.
	Note that the value can depend not only on the assignment
	but on the current values of the switches,
	if the {@link BooleanFormula}s contain any
	{@link BooleanFormula#makeSwitchableConst switchable constants}. */
    public abstract String interpret(boolean[] assignment_);

    /**
     * Mutate this {@link Transl} by {@link BooleanFormula#simplify simplifying} each {@link BooleanFormula}
     * in the Transl with the given partial assignment.
     */
    abstract public void simplify(PartialAssignment pa_);

    public String interpret(PartialAssignment pa_) { return interpret(pa_.toBoolArray()); }

    /**
     * @return <code>true</code> iff the values of all {@link BooleanFormula}s in this
     * {@link Transl} are {@link BooleanFormula#isConst constant}.
     */
    public boolean isConstant() {
	for (Iterator boolFormIter = getBooleanFormulas().iterator(); boolFormIter.hasNext();)
	    if (!((BooleanFormula)boolFormIter.next()).isConst())
		return false;
	return true;
    }

    /**
     * @return <code>true</code> iff the values of all {@link BooleanFormula}s in this
     * {@link Transl} are {@link BooleanFormula#isConstantValued constant-valued}.
     */
    public boolean isConstantValued() {
	for (Iterator boolFormIter = getBooleanFormulas().iterator(); boolFormIter.hasNext();)
	    if (!((BooleanFormula)boolFormIter.next()).isConstantValued())
		return false;
	return true;
    }
    

}
